package com.threegroup.admin.controller;

import com.threegroup.admin.annotation.AuthorityVerify.AuthorityVerify;
import com.threegroup.admin.dto.SysRoleDTO;
import com.threegroup.admin.dto.SysUserDTO;
import com.threegroup.admin.query.SysRoleQuery;
import com.threegroup.admin.query.SysUserQuery;
import com.threegroup.admin.service.ISysRoleService;
import com.threegroup.common.utils.Result;
import com.threegroup.common.validator.ValidatorUtils;
import com.threegroup.common.validator.group.DefaultGroup;
import com.threegroup.common.validator.group.UpdateGroup;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 角色列表查询
     * @return
     */
    @GetMapping("/list")
    public Result<List<SysRoleDTO>> list() {

        List<SysRoleDTO> sysRoleDTOList = sysRoleService.list();

        return new Result<List<SysRoleDTO>>().ok(sysRoleDTOList);
    }

    /**
     * 分页查询角色
     * @param sysRoleQuery 查询条件
     * @return
     */
    @AuthorityVerify("sys:role:page")
    @GetMapping("/page")
    public Result<Map<String, Object>> userList(SysRoleQuery sysRoleQuery) {

        return sysRoleService.getRoleList(sysRoleQuery);
    }

    /**
     * 根据角色id查询角色
     * @param id 角色id
     * @return
     */
    @AuthorityVerify("sys:role:info")
    @GetMapping("/{id}")
    public Result<SysRoleDTO> infoById(@PathVariable Long id) {

        SysRoleDTO sysRoleDTO = sysRoleService.getRoleInfoById(id);

        return new Result<SysRoleDTO>().ok(sysRoleDTO);
    }

    /**
     * 修改用户信息
     * @param sysRoleDTO 用户信息
     * @return
     */
    @AuthorityVerify("sys:role:update")
    @PutMapping
    public Result<String> update(@RequestBody SysRoleDTO sysRoleDTO) {

        // 校验数据合法性
        ValidatorUtils.validateEntity(sysRoleDTO, UpdateGroup.class, DefaultGroup.class);

        sysRoleService.update(sysRoleDTO);

        return new Result<>();
    }

}
